package com.bit.demo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @PostMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {
        // 1.参数校验
        // 2.验证密码是否正确
        // 3.进行返回
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }

        if ("admin".equals(userName) && "admin".equals(password)) {
            session.setAttribute("loginUserName", userName);
            return true;
        }
        return false;
    }

    @GetMapping("/getLoginUser")
    public String getLoginUser(HttpSession session) {
        return (String)session.getAttribute("loginUserName");
    }

//    @GetMapping("/getLoginUser")
//    public String getLoginUse(HttpServletRequest request) {
//        HttpSession session = request.getSession(false);
//        if (session == null) {
//            return "";
//        }
//        return(String)session.getAttribute("loginUserName");
//    }
}
